<template>
  <div class="loginPage">
    <div class="loginForm">
      <div class="loginFormtTop">
        <h2>登录页面</h2>
      </div>
      <el-form
        ref="login"
        :model="user"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="账号" prop="userName">
          <el-input v-model="user.userName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="密码" prop="passWord">
          <el-input v-model="user.passWord" type="password" autocomplete="off" />
        </el-form-item>
        <el-form-item label="选择登录身份">
          <el-radio-group v-model="role">
            <el-radio label="admin" size="large">管理员登录</el-radio>
            <el-radio label="anyUser" size="large">普通用户登录</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="滑动验证">
          <slider-verification></slider-verification>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="userLogin">登录</el-button>
          <el-button @click="resetForm">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { userLogin, AnyUser, adminUser } from '@a/login'
import SliderVerification from '@/components/SliderVerification.vue'
export default {
  name: 'loginPage',
  data () {
    return {
      rules: {
        userName: [
          {
            required: true,
            message: '用户名必须填写'
          },
          {
            pattern: /^[a-zA-Z]\w{3,7}$/,
            message: '账号由4-8位数字字母下划线组成,必须字母开头'
          }
        ],
        passWord: [
          {
            required: true,
            message: '密码必须填写'
          },
          {
            pattern: /^[a-zA-Z]\w{5,11}$/,
            message: '密码由6-11位数字字母下划线组成,必须字母开头'
          }
        ]
      },
      user: {
        userName: '',
        passWord: ''
      },
      role: 'admin'
    }
  },
  methods: {
    userLogin () {
      // 用户登录
      this.$refs.login.validate().then(() => {
        // 提交
        userLogin(this.user).then(res => {
          if (res.data.code === 200) {
            const { token, userInfo, role } = res.data.data
            // 立即缓存 为了 持久化
            localStorage.setItem('token', token)
            localStorage.setItem('userInfo', JSON.stringify(userInfo))
            localStorage.setItem('role', this.role)
            // 存储到vuex中
            this.$store.commit('user/INIT_USER_INFO', res.data.data)
            // 请求当前用户可以访问 权限接口
            if (this.role === 'admin') {
              // 管理员
              adminUser({
                role
              }).then(res => {
                if (res.data.code === 200) {
                  // 缓存
                  localStorage.setItem('nav', JSON.stringify(res.data.data.userPower))
                  localStorage.setItem('authRouter', JSON.stringify(res.data.data.authRouter))
                  // 动态存储侧边导航数据
                  this.$store.commit('provideRouter/INIT_PERMISSION', res.data.data)
                  // 根据返回 动态路由数组 动态添加路由
                  this.$store.commit('provideRouter/ADD_AUTH_ROUTES')
                  // 跳转到dashBoard
                  this.$router.replace('/dashBoard')
                }
              })
            } else {
              // 普通用户
              AnyUser({
                role
              }).then(res => {
                if (res.data.code === 200) {
                  // 缓存
                  localStorage.setItem('nav', JSON.stringify(res.data.data.userPower))
                  localStorage.setItem('authRouter', JSON.stringify(res.data.data.authRouter))
                  // 动态存储侧边导航数据
                  this.$store.commit('provideRouter/INIT_PERMISSION', res.data.data)
                  // 根据返回 动态路由数组 动态添加路由
                  this.$store.commit('provideRouter/ADD_AUTH_ROUTES')
                  // 跳转到dashBoard
                  this.$router.replace('/dashBoard')
                }
              })
            }
          }
        })
      })
    },
    resetForm () {
      // 重置表单
      this.$refs.login.resetFields()
    }
  },
  components: {
    SliderVerification
  }
}
</script>
<style lang="scss" scoped>
.loginPage {
  // 背景图全屏自适应
  background-image: url(../../assets/img/starImg.png);
  background-repeat: no-repeat;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  min-width: 1000px;
  z-index: -10;
  zoom: 1;
  background-color: #fff;
  background-repeat: no-repeat;
  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
  background-position: center 0;
  text-align: center;
  & h2{
    line-height: 100%;
    width: 100%;
    vertical-align: middle;
    text-align: center;
    color: white;
    font-size:40px;
    position: relative;
    top:30%;
  }
  .loginForm{
    background-color:rgba(0, 0, 0, 0.5) ;
    height: 500px;
    width: 700px;
    margin:100px auto;
    position: relative;
  }
}
.loginFormtTop{
  height: 100px;
}
.el-form{
  display: flex;
  flex-direction: column;
  width: 400px;
  height: 300px;
  margin: auto;
  .el-form-item{
    flex:1;
    width:100%;
    display: flex;
    height: 100%;
  }
  .el-button{
    margin-left: 25px;
    display: block;
  }
}
</style>
